'''
Case Type： 故障&可靠性测试
Case Name： 目录已存在	双机创建表空间，主机对应目录已经存在；备机目录已经存在
Create At: 2020/07/02
@zou_jialiang0505328114
Descption:
1. 主机创建目录
2.使用创建的目录创建表空间
3.备机创建目录
4.使用备机创建的目录创建表空间
'''
import os
import unittest
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_standby = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case029.py start------------')
        self.rootNode = Node()
        self.primary_dbuser = Node(node='PrimaryDbUser')
        self.primary_root = Node(node='PrimaryRoot')
        self.standby1_root = Node(node='Standby1Root')
        self.standby1_dbuser = Node(node='Standby1DbUser')
        self.Constant = Constant()
        self.path = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH), 'reliability_dbsys029')
        self.path_standby = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH), 'reliability_dbsys029_standby')

    def test_startdb(self):
        self.log.info('-----------------主机创建目录---------------------------')
        msg = self.primary_dbuser.sh('mkdir -p ' + self.path).result()
        self.log.info(msg)
        msg = self.primary_dbuser.sh(f'ls -al {self.path}').result()
        self.log.info(msg)

        self.log.info('------------------------------create tablespace---------------------')
        createdb = f'''drop tablespace if exists reliability_dbsys029;create tablespace reliability_dbsys029 location '{self.path}';'''
        createdbresult = self.sh_primy.executDbSql(createdb)
        self.log.info(createdb)
        self.log.info(createdbresult)
        self.assertIn('CREATE TABLESPACE', createdbresult)
        self.assertNotIn('cannot', createdbresult)
        self.assertNotIn(self.Constant.SQL_WRONG_MSG[1], createdbresult)

        time.sleep(2)

        self.log.info('-----------------备机创建目录---------------------------')
        self.standby1_dbuser.sh('mkdir -p' + self.path_standby)
        msg = self.standby1_dbuser.sh(f'ls -al {self.path_standby}').result()
        self.log.info(msg)

        self.log.info('------------------------------create tablespace---------------------')
        createdb1 = f'''drop tablespace if exists reliability_dbsys029_by;create tablespace reliability_dbsys029_by location '{self.path_standby}';'''
        createdbresult1 = self.sh_primy.executDbSql(createdb1)
        self.log.info(createdb1)
        self.log.info(createdbresult1)
        self.assertIn('CREATE TABLESPACE', createdbresult1)
        self.assertNotIn('cannot', createdbresult)
        self.assertNotIn(self.Constant.SQL_WRONG_MSG[1], createdbresult)

    def tearDown(self):
        self.log.info('------------------------------this is tearDown---------------------')
        self.log.info('------------------------------drop table space---------------------')
        cmd = f'''drop tablespace reliability_dbsys029_by;drop tablespace reliability_dbsys029;'''
        msg = self.sh_primy.executDbSql(cmd)
        self.log.info(cmd)
        self.log.info(msg)

        self.log.info('------------------------------delete file---------------------')
        self.primary_dbuser.sh('rm -rf ' + self.path)
        self.primary_dbuser.sh('rm -rf ' + self.path_standby)
